<?php

namespace app\index\controller;

use app\common\controller\Frontend;
use app\common\library\Token;
use EasyWeChat\Factory;
class Index extends Frontend
{

    protected $noNeedLogin = '*';
    protected $noNeedRight = '*';
    protected $layout = '';

    public function _initialize()
    {
        parent::_initialize();
    }

    public function index()
    {
        return $this->view->fetch();
    }

    public function wechatauth(){
        $url = \wechat\Wechat::getUrl();
//        $heder = "location:" . $url;
//        header($heder);
        $curl = new \Curl\Curl();
        $curl->get($url);
    }

    public function news()
    {
        $newslist = [];
        return jsonp(['newslist' => $newslist, 'new' => count($newslist), 'url' => 'https://www.fastadmin.net?ref=news']);
    }

    public function test(){


        $config = [
            'app_id' => 'wx3cf0f39249eb0exx',
            'secret' => 'f1c242f4f28f735d4687abb469072axx',

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
        ];

        $app = Factory::officialAccount($config);
    }

    public function test1(){

        $config = [

            'app_id' => 'wx3cf0f39249eb0exx',
            'secret' => 'f1c242f4f28f735d4687abb469072axx',

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
            'oauth' => [
                'scopes'   => ['snsapi_userinfo'],
                'callback' => '/index/test2',
            ],

        ];

        $app = Factory::officialAccount($config);

//        dump($app);exit;
        $oauth = $app->oauth;

// 未登录
        if (empty($_SESSION['wechat_user'])) {

            $_SESSION['target_url'] = '/index/test1';

            return $oauth->redirect();
            // 这里不一定是return，如果你的框架action不是返回内容的话你就得使用
//             $oauth->redirect()->send();

        }

// 已经登录过
        $user = $_SESSION['wechat_user'];
    }

    public function test2(){
        $config = [
            'app_id' => 'wx3cf0f39249eb0exx',
            'secret' => 'f1c242f4f28f735d4687abb469072axx',

            // 指定 API 调用返回结果的类型：array(default)/collection/object/raw/自定义类名
            'response_type' => 'array',

            'log' => [
                'level' => 'debug',
                'file' => __DIR__.'/wechat.log',
            ],
        ];

        $app = Factory::officialAccount($config);
        $oauth = $app->oauth;

// 获取 OAuth 授权结果用户信息
        $user = $oauth->user();

        $_SESSION['wechat_user'] = $user->toArray();

        $targetUrl = empty($_SESSION['target_url']) ? '/' : $_SESSION['target_url'];

//        header('location:'. $targetUrl); // 跳转到 user/profile
    }

    public function testall(){
        $user = \app\common\model\User::all();
//        dump($user);
        foreach($user as $k => $v){
            dump($v);exit;

        }
    }

    public function testp(){
        $model = new \app\common\model\Type();
        $list = $model->getList();
        dump($list);
    }



}
